﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;

namespace DnnDash.DesktopDashboard
{
    internal class DnnRequest
    {
        internal static string GetRequest(string url, string username, string password
                ,out HttpStatusCode statusCode, out string errorMsg)
        {
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            wr.Credentials = new NetworkCredential(username, password);
            CookieContainer cookies = null;
            string responseString = DoRequest(wr, out statusCode, out errorMsg, ref cookies);
            return responseString;
        }
        internal static string GetRequest(string url, out HttpStatusCode statusCode, out string errorMsg, ref CookieContainer cookies)
        {
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            if (cookies != null)
                wr.CookieContainer = cookies;
            //wr.Credentials = new NetworkCredential(username, password);
            string responseString = DoRequest(wr, out statusCode, out errorMsg, ref cookies);
            return responseString;
        }
        internal static string GetRequest(string url, string username, string password, out HttpStatusCode statusCode, out string errorMsg, ref CookieContainer cookies)
        {
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            if (cookies != null)
                wr.CookieContainer = cookies;
            wr.Credentials = new NetworkCredential(username, password);
            wr.Accept = "application/text";
            string responseString = DoRequest(wr, out statusCode, out errorMsg, ref cookies);
            return responseString;
        }
        internal static string GetXmlRequest(string url, string username, string password, out HttpStatusCode statusCode, out string errorMsg, ref CookieContainer cookies)
        {
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            if (cookies != null)
                wr.CookieContainer = cookies;
            wr.Credentials = new NetworkCredential(username, password);
            wr.Accept = "application/xml";
            string responseString = DoRequest(wr, out statusCode, out errorMsg, ref cookies);
            return responseString;
        }
        internal static SimpleResult GetJsonRequest(string url, string username, string password, out HttpStatusCode statusCode, out string errorMsg, ref CookieContainer cookies)
        {
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            if (cookies != null)
                wr.CookieContainer = cookies;
            wr.Credentials = new NetworkCredential(username, password);
            string responseString = DoRequest(wr, out statusCode, out errorMsg, ref cookies);
            DebugResult result = JsonSerializer.Deserialize<DebugResult>(responseString);
            //var badResponse = new { Message = string.Empty, MessageDetail = string.Empty };
            //dynamic r = JsonSerializer.Deserialize(responseString, badResponse.GetType());
            return result;
        }
        internal static string PostJsonRequest(string url, string username, string password, string body, out HttpStatusCode statusCode, out string errorMsg, ref CookieContainer cookies)
        {
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            wr.Credentials = new NetworkCredential(username, password);
            wr.Method = "POST";
            wr.ContentType = "application/json";
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(body);
            wr.ContentLength = byteArray.Length;
            using (System.IO.Stream dataStream = wr.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }
            string responseString = DoRequest(wr, out statusCode, out errorMsg, ref cookies);
            return responseString;
        }
        private static string DoRequest(HttpWebRequest wr, out HttpStatusCode statusCode, out string errorMsg, ref CookieContainer cookies)
        {
            string responseString = ""; errorMsg = null; statusCode = HttpStatusCode.NoContent;
            try
            {
                using (WebResponse response = wr.GetResponse())
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                    {
                        responseString = sr.ReadToEnd();
                    }
                    statusCode = ((HttpWebResponse)response).StatusCode;
                }
            }
            catch (System.Net.WebException ex)
            {
                HttpWebResponse errResponse = (HttpWebResponse)ex.Response;
                errorMsg = ex.Message;
                statusCode = errResponse.StatusCode;
                //read the response stream for the error message
                using (System.IO.StreamReader sr = new System.IO.StreamReader(errResponse.GetResponseStream()))
                {
                    responseString = sr.ReadToEnd();
                }
            }
            return responseString;
        }


        internal static string GetUrl(string portalAlias, string modulePath, string controllerName, string callName, bool secure)
        {

            string scheme = "";
            if (secure)
                scheme = Uri.UriSchemeHttps + Uri.SchemeDelimiter;
            else
                scheme = Uri.UriSchemeHttp + Uri.SchemeDelimiter;

            string url = string.Format("{0}{1}/DesktopModules/{2}/API/{3}/{4}", scheme, portalAlias, modulePath, controllerName, callName);

            return url;

        }
    }
}
